<template>
	<view>
		<view class="ui-all">
			<view class="ui-list">
				<text>账号</text>
				<input v-model="loginForm.username" placeholder="请输入账号" />
			</view>
			<view class="ui-list">
				<text>密码</text>
				<input v-model="loginForm.password" type="text" :password="isHidePassword ? true : false"
					placeholder="请输入密码" maxlength="20" />
				<image @click="isHidePasswordClick" class="eye"
					:src="isHidePassword ? '/static/img/attention.png' : '/static/img/attention_forbid.png'"></image>
			</view>
			<view class="ui-list" v-if="captchaOnOff">
				<text>验证码</text>
				<input v-model="loginForm.code" type="number" placeholder="请输入验证码" maxlength="20" />
				<image class="codeImg" :src="codeUrl" @click="getCode"></image>
			</view>
			<view class="register">
				<navigator url="/pages/register/register" hover-class="none">注册</navigator>
			</view>
		</view>
		<u-toast ref="uToast" />
		<button class="login mt5" @click="submit">登录</button>
	</view>
</template>
<script>
	import {
		checkPwd,
		checkShenfenzheng
	} from '@/common/validate.js';
	import {
		getCodeImg,
		getSignatureInfo
	} from '@/api/login';
	import {
		updateUser
	} from '@/api/user.js';
	export default {
		data() {
			return {
				isHidePassword: true,
				codeUrl: '',
				captchaOnOff: true,
				loginForm: {
					username: this.$store.state.userInfo.userName,
					password: '',
					rememberMe: false,
					code: '',
					uuid: ''
				},
				register: false,
				userExtend2: ''
			};
		},
		onLoad() {
			const isdingding = () => {
				//判断是不是钉钉
				var ua = navigator.userAgent.toLowerCase();
				return ua.indexOf("dingtalk") >= 0;
			};
			if (isdingding) {
				this.ddLogin();
			} else {
				// #ifdef APP-PLUS
				// 获取客户端标识
				let pinf = plus.push.getClientInfo();
				if (pinf && pinf.clientid) uni.setStorageSync('cid', pinf.clientid);
				else {
					var obtainingCIDTimer = setInterval(function() {
						pinf = plus.push.getClientInfo();
						if (pinf.clientid) {
							uni.setStorageSync('cid', pinf.clientid);
							clearInterval(obtainingCIDTimer);
						}
					}, 50);
				}
				setTimeout(() => {
					console.log('cid', uni.getStorageSync('cid'));
				}, 500);
				// #endif
				this.getCode();
			}

		},
		methods: {
			// 钉钉登录
			ddLogin() {
				let that = this
				getSignatureInfo(location.href).then(res => {
					const _config = res.data
					//console.log(res.data)
					var jsapilist = [
						'device.notification.alert', //警告框
						'device.notification.confirm', //确认框
						'biz.navigation.setTitle', //设置导航栏标题
						'biz.navigation.quit', //关闭页面
						'biz.util.openModal', //打开弹窗（模态窗）
						'biz.contact.complexPicker', //选择人员
						'device.geolocation.get', //获取当前地理位置信息
						'biz.map.locate', //地图定位
						'biz.map.search', //地图页面支持搜索
						'biz.map.view' //展示位置
					] //需要使用的jsapi列表

					//1、鉴权
					window.dd.config({
						agentId: _config.agentId,
						corpId: _config.corpId, //必填，企业ID
						timeStamp: _config.timeStamp, // 必填，生成签名的时间戳
						nonceStr: _config.nonceStr, // 必填，生成签名的随机串
						signature: _config.signature, // 必填，签名
						jsApiList: jsapilist // 必填，需要使用的jsapi列表，注意：不要带dd。
					});

					window.dd.error(function(err) { //验证失败  
						console.log("进入到error中");
						console.log('dd error: ' + JSON.stringify(err));
					})
					// dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
					window.dd.runtime.permission.requestAuthCode({
						corpId: _config.corpId,
						onSuccess: function(result) {
							that.toast('登录中,请稍后...');
							console.log(result.code);
							that.$store.dispatch('DDLogin', result.code).then(() => {
									that.$store.dispatch('GetInfo').then(res => {
										let param = {
											userExtend2: uni.getStorageSync('cid')
										};
										updateUser(param).then(res => {
											uni.reLaunch({
												url: '/pages/index/index'
											});
										});
									});
								})
								.catch((res) => {
									console.log(res)
									that.toast('登录异常,请联系管理员排查系统账号和钉钉账号对应关系');
								});
						},
						onFail: function(err) {
							alert('err:')
							alert(err)
						}
					});
				});
			},
			isHidePasswordClick() {
				this.isHidePassword = !this.isHidePassword;
			},
			getCode() {
				getCodeImg().then(res => {
					this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
					if (this.captchaOnOff) {
						this.codeUrl = 'data:image/gif;base64,' + res.data.img;
						this.loginForm.uuid = res.data.uuid;
					}
				});
			},
			submit() {
				if (!this.loginForm.username) {
					this.toast('请填写账号');
					return;
				}
				if (!this.loginForm.password) {
					this.toast('请填写密码');
					return;
				}
				this.toast('登录中...');
				this.$store
					.dispatch('Login', this.loginForm)
					.then(() => {
						this.$store.dispatch('GetInfo').then(res => {
							let param = {
								userExtend2: uni.getStorageSync('cid')
							};
							updateUser(param).then(res => {
								uni.reLaunch({
									url: '/pages/index/index'
								});
							});
						});
					})
					.catch(() => {
						if (this.captchaOnOff) {
							this.getCode();
						}
					});
			}
		}
	};
</script>
<style lang="scss">
	.eye {
		position: absolute;
		height: 50rpx;
		width: 50rpx;
		right: 0;
		top: 50%;
		transform: translateY(-50%);
	}

	.register {
		margin-top: 20rpx;
		color: #69c8fa;
		text-align: right;
	}

	.tishi {
		font-size: 20rpx;
		color: gray;
	}

	.login {
		width: 200rpx;
		color: white;
		height: 75rpx;
		font-size: 30rpx;
		background-color: #69c8fa;
	}

	.codeImg {
		height: 64rpx;
		position: absolute;
		right: 0;
		top: 10rpx;
		width: 200rpx;
	}
</style>
